package com.gxa.autoparts.portal.controller;

import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.portal.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;

import java.util.Map;
import java.util.Set;

/**
 * @author ：looker
 * @date ：Created in 2022/1/6 15:29
 */
@RestController
@Api(tags = "商品检索器模块")
@Transactional(rollbackFor = Throwable.class)
@RequestMapping("/search")
public class SearchController {

    @Autowired
    SearchService searchService;

    @PostMapping("/gethistory")
    @ApiOperation("获取用户搜商品历史")
    public Map getHistory(){
        Set<Object> searchHistoryByUserId = searchService.getSearchHistoryByUserId();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(searchHistoryByUserId);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getHotSearch")
    @ApiOperation("获取热门搜索")
    public Map getHotSearch(){
        Set<Object> hotList = searchService.getHotList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(hotList);
        return jsonResult.getDataMap();
    }

    @PostMapping("/delHistory")
    @ApiOperation("删除搜索历史记录")
    public Map delHistory(){
        searchService.delSearchHistoryByUserId();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功！");
        return jsonResult.getDataMap();
    }
}
